﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;

namespace VNWIKI_WEB
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
            routes.MapRoute(
                    "Default", // Route name
                    "", // URL with parameters
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );
            routes.MapRoute(
                  "AlbumView", // Route name
                  "{category_id}/Album/{album_id}.html", // URL with parameters
                  new { controller = "Home", action = "AlbumView", category_id = string.Empty, album_id = string.Empty } // Parameter defaults
              );

            routes.MapRoute(
                  "Search", // Route name
                  "Search/{key}", // URL with parameters
                  new { controller = "Home", action = "Search", key = string.Empty } // Parameter defaults
              );

            routes.MapRoute(
                  "MyProfile", // Route name
                  "MyProfile", // URL with parameters
                  new { controller = "Home", action = "MyProfile"} // Parameter defaults
              );
            routes.MapRoute(
                  "MyProfileEdit", // Route name
                  "MyProfile/Edit", // URL with parameters
                  new { controller = "Home", action = "MyProfileEdit" } // Parameter defaults
              );

            routes.MapRoute(
                  "MyAlbums", // Route name
                  "MyAlbums", // URL with parameters
                  new { controller = "Home", action = "MyAlbums"} // Parameter defaults
              );

            routes.MapRoute(
                  "CategoryView", // Route name
                  "{catID}.html", // URL with parameters
                  new { controller = "Home", action = "GetAlbumList", catID = string.Empty } // Parameter defaults
              );

            routes.MapRoute(
                "login", // Route name
                "Login", // URL with parameters
                new { controller = "Account", action = "SignIn", accName = String.Empty, password = string.Empty } // Parameter defaults
            );

            routes.MapRoute(
               "logout", // Route name
               "Logout", // URL with parameters
               new { controller = "Account", action = "Logout" } // Parameter defaults
           );

            routes.MapRoute(
              "signup", // Route name
              "SignUp", // URL with parameters
              new { controller = "Account", action = "SignUp" } // Parameter defaults
          );
            routes.MapRoute(
                "upload", //Route name
                "Upload", //URL with paramaters
                new { controller = "Upload", action = "Index"} //Parameter defaults
                );
            routes.MapRoute(
                "InsertAlbum",
                "Upload/InsertAlbum/",
                new { controller="Upload", action = "InsertAlbum", albumName=String.Empty, catID=0, tags=String.Empty}
                );
            routes.MapRoute(
                "ReceiveFileUpload",
                "Upload/ReceiveFileUpload/",
                new { controller = "Upload", action = "ReceiveFileUpload"}
                );

            //Admin route
            routes.MapRoute(
                "Admin",
                "Admin",
                new { controller = "Admin", action = "Index"}
                );
            routes.MapRoute(
                "LogOn",
                "Admin/LogOn",
                new { controller = "Admin", action = "LogOn" }
                );
            routes.MapRoute(
                "LogOff",
                "Admin/LogOff",
                new { controller = "Admin", action = "LogOff" }
                );
            routes.MapRoute(
                "UsersManager",
                "Admin/UserManager",
                new { controller = "Admin", action = "UsersManager" }
                );
            routes.MapRoute(
                "CategoriesManager",
                "Admin/CategoriesManager",
                new { controller = "Admin", action = "CategoriesManager" }
                );
            routes.MapRoute(
                "AlbumsManager",
                "Admin/AlbumsManager",
                new { controller = "Admin", action = "AlbumsManager" }
                );
            routes.MapRoute(
                "PhotosManager",
                "Admin/PhotosManager",
                new { controller = "Admin", action = "PhotosManager" }
                );

            routes.MapRoute(
                "UserList",
                "Admin/User/List",
                new { controller = "User", action = "Index" }
                );
            routes.MapRoute(
                "UserCreate",
                "Admin/User/Create",
                new { controller = "User", action = "Create" }
                );
            routes.MapRoute(
                "UserEdit",
                "Admin/User/Edit",
                new { controller = "User", action = "Edit" }
                );
            routes.MapRoute(
                "UserDetails",
                "Admin/User/Details",
                new { controller = "User", action = "Details" }
                );
            routes.MapRoute(
                "UserDelete",
                "Admin/User/Delete",
                new { controller = "User", action = "Delete" }
                );
            routes.MapRoute(
                "CategoryList",
                "Admin/Category/List",
                new { controller = "Category", action = "Index" }
                );
            routes.MapRoute(
                "CategoryCreate",
                "Admin/Category/Create",
                new { controller = "Category", action = "Create" }
                );
            routes.MapRoute(
                "CategoryEdit",
                "Admin/Category/Edit",
                new { controller = "Category", action = "Edit" }
                );
            routes.MapRoute(
                "CategoryDetails",
                "Admin/Category/Details",
                new { controller = "Category", action = "Details" }
                );
            routes.MapRoute(
                "CategoryDelete",
                "Admin/Category/Delete",
                new { controller = "Category", action = "Delete" }
                );
            routes.MapRoute(
                "AlbumList",
                "Admin/Album/List",
                new { controller = "Album", action = "Index" }
                );
            routes.MapRoute(
                "AlbumCreate",
                "Admin/Album/Create",
                new { controller = "Album", action = "Create" }
                );
            routes.MapRoute(
                "AlbumEdit",
                "Admin/Album/Edit",
                new { controller = "Album", action = "Edit" }
                );
            routes.MapRoute(
                "AlbumDetails",
                "Admin/Album/Details",
                new { controller = "Album", action = "Details" }
                );
            routes.MapRoute(
                "AlbumDelete",
                "Admin/Album/Delete",
                new { controller = "Album", action = "Delete" }
                );
            routes.MapRoute(
                "PhotoList",
                "Admin/Photo/List",
                new { controller = "Photo", action = "Index" }
                );
            routes.MapRoute(
                "PhotoCreate",
                "Admin/Photo/Create",
                new { controller = "Photo", action = "Create" }
                );
            routes.MapRoute(
                "PhotoEdit",
                "Admin/Photo/Edit",
                new { controller = "Photo", action = "Edit" }
                );
            routes.MapRoute(
                "PhotoDetails",
                "Admin/Photo/Details",
                new { controller = "Photo", action = "Details" }
                );
            routes.MapRoute(
                "PhotoDelete",
                "Admin/Photo/Delete",
                new { controller = "Photo", action = "Delete" }
                );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Session_Start()
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */
            try
            {
                string session_param_name = "ASPSESSID";
                string session_cookie_name = "ASP.NET_SessionId";

                if (HttpContext.Current.Request.Form[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
                }
            }
            catch
            {
            }

            try
            {
                string auth_param_name = "AUTHID";
                string auth_cookie_name = FormsAuthentication.FormsCookieName; 
                
                if (HttpContext.Current.Request.Form[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
                }

            }
            catch
            {
            }
        }

        private void UpdateCookie(string cookie_name, string cookie_value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
            if (null == cookie)
            {
                cookie = new HttpCookie(cookie_name);
            }
            cookie.Value = cookie_value;
            HttpContext.Current.Request.Cookies.Set(cookie);
        }
    }
}